<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class c_entry extends CI_Controller {

	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model("m_entry");
	}
	
	function index(){
		$task = $this->input->post("task");
		switch($task){
			case 'LIST_JADWAL' : 
				$this->list_jadwal(); 
				break;
			case 'SAVE' : 
				$this->save(); 
				break;
			case 'DELETE' : 
				$this->delete(); 
				break;
			case 'REVISI' : 
				$this->revisi(); 
				break;
			case 'HISTORY' : 
				$this->history(); 
				break;
			case 'LASTREVISI' :
				$this->getLastRevisi(); 
				break;
			case 'owner' :
				$this->_owner(); 
				break;
			default : 
				$data['data_owner'] = $this->m_entry->getOwner("combo");
				$data['data_cabang'] = $this->m_entry->getCabang("combo");
				$data['data_kondisi'] = $this->m_entry->getKondisi("combo");
				$data['data_pic'] = $this->m_entry->getPic("combo");
				$this->load->view('v_entry',$data);
		}
	}
	
	function list_jadwal(){
		$jadwal_namaproyek = mysql_real_escape_string($this->input->post('jadwal_namaproyek'));
		$page = $this->input->post('page');
		$rows = $this->input->post('rows');
		$id = $this->input->post('jadwal_id');
		$option = $this->input->post('option');
		$tipe = $this->input->post('tipe');
		$data = $this->m_entry->list_jadwal($option,$id,$page,$rows , $jadwal_namaproyek, $tipe );
		echo $data;
	}

	function save(){
 		$id = $this->input->post("hidden-jadwalid");
		$data = array(
			"m_cabang_id" => $this->input->post("cmb-cabang") ,
			"m_owner_id" => $this->input->post("cmb-owner") ,
			"jadwal_koordinator" => $this->input->post("txt-koordinator") ,
			"jadwal_namaproyek" => $this->input->post("txt-namaproyek") , 
			"jadwal_alias" => $this->input->post("txt-namaproyekalias") , 
			"m_kondisi_id" => $this->input->post("cmb-kondisi") ,
			"jadwal_status" => $this->input->post("cmb-status") , 
			"jadwal_nodoc" => $this->input->post("txt-doc") , 
			"jadwal_lokasi" => $this->input->post("txt-lokasidoc") , 
			"jadwal_nk" => $this->input->post("txt-nk") ,
			"jadwal_pagu" => $this->input->post("txt-pagu") ,
			"jadwal_flagcr" => $this->input->post("check-cr") ,
			"jadwal_tglcr" => date("Y-m-d",strtotime($this->input->post("date-cr"))) ,
			"jadwal_flagmr" => $this->input->post("check-mr") ,
			"jadwal_flagijin" => $this->input->post("check-ijin") ,
			"jadwal_flagundangan" => $this->input->post("check-undangan") ,
			"jadwal_flagllg" => $this->input->post("check-llg") ,
			"jadwal_flagboq" => $this->input->post("check-boq") ,
			"jadwal_flaqrks" => $this->input->post("check-rks") ,
			"jadwal_flaggbr" => $this->input->post("check-gbr") ,
			"jadwal_nolapor" => $this->input->post("check-nolapor") ,
			"jadwal_tglaan" => date("Y-m-d",strtotime($this->input->post("date-aanwijz"))) ,
			"jadwal_tglpq" => date("Y-m-d",strtotime($this->input->post("date-pq"))) ,
			"jadwal_tglpemasukan" => date("Y-m-d",strtotime($this->input->post("date-pemasukan"))) ,
			"jadwal_tglpemasukan2" => date("Y-m-d",strtotime($this->input->post("date-pemasukan2"))) ,
			"jadwal_tglklarifikasi" => date("Y-m-d",strtotime($this->input->post("date-klarifikasi"))) ,
			"jadwal_tglkontrak" => date("Y-m-d",strtotime($this->input->post("date-kontrak"))) ,
			"jadwal_remkontrak" => $this->input->post("txt-remark") ,
			"jadwal_updatestatus" => $this->input->post("txt-updatestatus"),
			"jadwal_prabrain" => date("Y-m-d",strtotime($this->input->post("date-prabrain"))) ,
			"jadwal_brain1" => date("Y-m-d",strtotime($this->input->post("date-brain1"))) ,
			"jadwal_brain2" => date("Y-m-d",strtotime($this->input->post("date-brain2"))) ,
			"jadwal_finalisasi" => date("Y-m-d",strtotime($this->input->post("date-final"))) ,
			"jadwal_cekdok" => date("Y-m-d",strtotime($this->input->post("date-cekdok"))) ,
			"jadwal_picanggaran" => $this->input->post("cmb-anggaran") ,
			"jadwal_picmetode" => $this->input->post("cmb-metode") ,
			"jadwal_picme" => $this->input->post("cmb-me") ,
			"jadwal_picharga" => $this->input->post("cmb-harga") ,
			"jadwal_picvolume" => $this->input->post("cmb-volume") ,
			"jadwal_picexpert" => $this->input->post("cmb-expert"),
			"jadwal_lastupdate" => date("Y-m-d h:i:s")
		);
		
		if ($this->input->post("pqtopasca") == "true"){
			$data_grid = $this->m_entry->datagrid($id);
			$datapqkepasca = array(
				"m_jadwal_id" => $id ,
				"m_cabang_id" => $data_grid->m_cabang_id,
				"m_owner_id" => $data_grid->m_owner_id ,
				"jadwalhistory_koordinator" => $data_grid->jadwal_koordinator ,
				"jadwalhistory_namaproyek" => $data_grid->jadwal_namaproyek , 
				"jadwalhistory_alias" => $data_grid->jadwal_alias , 
				"m_kondisi_id" => $data_grid->m_kondisi_id ,
				"jadwalhistory_nodoc" => $data_grid->jadwal_nodoc ,  
				"jadwalhistory_nk" => $data_grid->jadwal_nk ,
				"jadwalhistory_pagu" => $data_grid->jadwal_pagu ,
				"jadwalhistory_flagcr" => $data_grid->jadwal_flagcr ,
				"jadwalhistory_tglcr" => $data_grid->jadwal_tglcr ,
				"jadwalhistory_flagmr" => $data_grid->jadwal_flagmr ,
				"jadwalhistory_flagijin" => $data_grid->jadwal_flagijin ,
				"jadwalhistory_flagundangan" => $data_grid->jadwal_flagundangan ,
				"jadwalhistory_flagllg" => $data_grid->jadwal_flagllg ,
				"jadwalhistory_flagboq" => $data_grid->jadwal_flagboq ,
				"jadwalhistory_flaqrks" => $data_grid->jadwal_flaqrks ,
				"jadwalhistory_flaggbr" => $data_grid->jadwal_flaggbr ,
				"jadwalhistory_tglpq" => $data_grid->jadwal_tglpq ,
				"jadwalhistory_remkontrak" => $data_grid->jadwal_remkontrak ,
				"jadwalhistory_updatestatus" => $data_grid->jadwal_updatestatus,
				"jadwalhistory_lastupdate" => $data_grid->jadwal_lastupdate
			);
			$this->m_entry->jadwalhistory($datapqkepasca); 
		}
		
		echo $this->m_entry->save($id,$data); 
	}
	
	function delete(){
		$data = array(
			"jadwal_id" => $this->input->post("jadwal_id")
		);
		$this->m_entry->delete($data);
	}
	
	function revisi(){
		$jadwal_id = $this->input->post("revisiid");
		$data = array(
			"t_jadwal_id" => $jadwal_id,
			"revisi_harga" => $this->input->post("txt-revisiharga"),
			"revisi_keterangan" => $this->input->post("txt-revisiketerangan"),
			"revisi_tgl" => date("Y-m-d",strtotime($this->input->post("date-revisitgl"))),
			"revisi_ke" => $this->m_entry->getRevisiKe($jadwal_id)
		);
		$this->m_entry->revisi($data);
	}
	
	function history(){
		$jadwal_id = $this->input->post("historyid");
		$data = array(
			"t_jadwal_id" => $jadwal_id,
			"history_tgl" => date("Y-m-d",strtotime($this->input->post("date-historytgl"))),
			"history_keterangan" => $this->input->post("txt-historyketerangan"),
			"history_jenistgl" => $this->input->post("historyjenis"),
			"history_tglcreated" => date("Y-m-d")
		);
		$this->m_entry->history($data);
		$result = array(
			"status" => "success",
			"tgl" => date("Y-m-d",strtotime($this->input->post("date-historytgl")))
		);
		echo json_encode($result);
	}
	
	function getLastRevisi(){
		$jadwal_id = $this->input->post("jadwal_id");
		$last_revisi =  $this->m_entry->getLastRevisi($jadwal_id);
		echo json_encode($last_revisi);
	}
	
	function _owner(){
		echo json_encode($this->m_entry->_owner());
	}
}